<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Pro;
use App\Att;
use App\Bid;
use DB;
use Auth;
use Validator;

class ProController extends Controller
{
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];

    public function jie() {
        return view('woyaojiekuan');
    }
    public function jiePost(Request $request) {
      //手动验证方法 1
        /*$this->validate($request,[
           'age'=>'required|in:15,40,80',
           'money'=>'required|digits_between:2,7',
           'mobile'=>'required|regex:/^1[3458]\d{9}$/',],

           [
            'money.required'=>'金额必须填写',
            'money.digits_between'=>'填的啥玩意?',
            'in'=>':attribute 必须是以下 :values 之一',

            'mobile.required'=>'请填写真实手机号码',
            'mobile.regex'=>':attribute 不符合要求'
           ]
        );
*/
     $validate = Validator::make($request->all(),
      //手动验证方法 2
       [
           'age'=>'required|in:15,40,80',
           'money'=>'required|digits_between:2,7',
           'mobile'=>'required|regex:/^1[3458]\d{9}$/',],

           [
            'money.required'=>'金额必须填写',
            'money.digits_between'=>'填的啥玩意?',
            'in'=>':attribute 必须是以下 :values 之一',

            'mobile.required'=>'请填写真实手机号码',
            'mobile.regex'=>':attribute 不符合要求'
           ]
      );
         if ($validate->fails()) {
              return back()->withErrors($validate)->withInput();
             }


        $pro = new Pro();
        $att = new Att();

        //把收到的金额转化为'分'
        $pro->uid = $request->user()->uid;
        $pro->name = $request->user()->name;
        $pro->money = intval($request->money) * 100;   //或者$_POST['money']
        $pro->mobile = $request->mobile;
        $pro->age = $request->age;
        $pro->pubtime = time();
        $pro->save();  //此时$pro对应新增的那一行

        //写附表
        $att->uid = $request->user()->uid;
        $att->pid = $pro->pid;
        $att->age = $request->age;
        $att->save();

       echo '申请已提交';
    }

    public function pro($pid){
       $pro = Pro::find($pid);
       return view('pro',['pro'=>$pro]);
    }

    public function touzi(Request $request,$pid){
      $bid = new Bid;

      $user = $request->user();
      $bid->uid = $user->uid;
      $bid->pid = $pid;
      $bid->title = $request->title;
      $bid->money = $request->money * 100;
      $bid->pubtime = time();

      $pro = Pro::find($pid);
      $con=$pro->recive += $bid->money;

      if ($con>$pro->money) {
        return '兄弟,你投多了!!!';
        exit();
      }
      $bid->save();
      $pro->save();

      if ($con== $pro->money) {
        $this->touziDone($pid);
      }
      return '购买成功';
      }

    protected function touziDone($pid){
       //1.修改还款项目状态2, 还款/收益中
       $pro = Pro::find($pid);
       $pro->status = 2;
       $pro->save();

       //2.为借款者生成还款记录
       $amount = ($pro->money * $pro->rate/1200) + ($pro->money/$pro->hrange);//每月应还的利息加本金
       $row = ['uid'=>$pro->uid,'pid'=>$pro->pid,'title'=>$pro->title,'amount'=>$amount,'status'=>0];

       $today = date('Y-m-d');
       for ($i=1; $i <= $pro->hrange; $i++) {
        $paydate = date('Y-m-d',strtotime("+ $i months"));
        $row['paydate'] = $paydate;
        DB::table('hks')->insert($row);
       }
       //3.为投资者产生收益记录
       $bids = Bid::where('pid',$pid)->get();
       $row = [];
       $row['title'] = $pro->title;
       $row['enddate'] = date('Y-m-d',strtotime("+ {$pro->hrange} months"));
       foreach ($bids as $b) {
         $row['uid'] = $b->uid;
         $row['pid'] = $b->pid;
         $row['amount'] = $b->money * $pro->rate/36500;
         DB::table('tasks')->insert($row);
       }

    }

     public function myzd(){
      $hks = DB::table('hks')->paginate(5);
      return view('myzd',['hks'=>$hks]);
    }

    public function mytz(){
      $user = Auth::user();
      $bids = Bid::where('bids.uid',$user->uid)->whereIn('status',[1,2])->join('projects','bids.pid', '=' , 'projects.pid')->get(['bids.*','projects.status']);
        //dd($bids);
      return view('mytz',['bids'=>$bids]);
    }`

    public function mysy(){
      $user = Auth::user();
      $grows = DB::table('grows')->where('uid',$user->uid)->orderBy('gid','desc')->get();

      return view('mysy',['grows'=>$grows]);
    }
}
